<style>

</style>
<div id="mainContent" class="easyui-layout" data-options="fit:true">
    <div data-options="region:'north'" style="overflow: hidden;height: 140px;">
        <div class="easyui-panel" data-options="border:0">
            <fieldset style="padding: 10px;margin: 4px; border:1px solid #f0ad4e">
                <legend>查询</legend>
                <div style="margin-bottom:20px;text-align: right">
                    <input id="winnerName" name="winnerName" class="easyui-textbox theme-textbox-radius" label="中奖者:"
                           data-options="prompt:'请输入中奖者姓名...'" style="width:60%;"/>
                </div>
                <div style="text-align: right">
                    <input id="startDate" name="startDate"
                           class="easyui-datebox theme-textbox-radius" label="开始日期:"
                           style="width:26%;"
                           data-options="prompt:'请选择开始日期',editable:false,required:false,formatter:dateFormatter,onSelect:onSelected"/>
                    <input id="endDate" name="endDate"
                           class="easyui-datebox theme-textbox-radius" label="结束日期:"
                           style="width:26%;"
                           data-options="prompt:'请选择结束日期',editable:false,required:false,formatter:dateFormatter,validType:'equalStartDate[\'#startDate\']'"/>
                    <a id="queryOrderBtn" href="#" class="easyui-linkbutton button-orange button-xs l-btn l-btn-small">
                        <span class="l-btn-left">
                            <span class="l-btn-text">
                                <i class="iconfont">&#xe6ac;</i> 查询
                            </span>
                        </span>
                    </a>
                </div>
            </fieldset>
        </div>
    </div>
    <div data-options="region:'center'">
        <table id="winnerLogDg" title="" class="easyui-datagrid"
               data-options="
                url: '${base}/admin/winner/dataList.do',
                fit:true,
                lines: true,
                rownumbers: false,
                striped: true,
                animate:true,
                showFooter: true,
                pagination: true,
                singleSelect: false,
                selectOnCheck: true,
                checkOnSelect: true,
                idField: 'winId',
                toolbar: '#tb',
                onLoadSuccess: loadSuccess,
                queryParams: {
                    uName: '',
                    startD: '',
                    endD: ''
                }
            " border="0">
            <thead data-options="frozen:true">
            <tr>
                <th data-options="field:'winId',hidden:true">&nbsp;</th>
                <th data-options="field:'userName'" width="5%">用户名</th>
                <th data-options="field:'goodsName'" width="5%">商品</th>
                <th data-options="field:'rewardName'" width="5%">奖品</th>
                <th data-options="field:'rewardPrice'" width="5%">价值</th>
                <th data-options="field:'shareCount'" width="5%">分享次数</th>
                <th data-options="field:'winTime'" width="10%">中奖时间</th>
                <th data-options="field:'winnerName'" width="8%">中奖者姓名</th>
                <th data-options="field:'winnerPhone'" width="5%">手机号码</th>
                <th data-options="field:'winnerAddress'" width="28%">地址</th>
                <th data-options="field:'dataStatus',formatter:fmtControl" width="12%">状态</th>
            </tr>
            </thead>
        </table>
    </div>
</div>

<div id="tb">
    <a href="javascript:updateWinner('2');" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">审核中奖者</a>
    <a href="javascript:updateWinner('3');" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true">发放奖品</a>
</div>


<script>

    $(function(){
        $('#queryOrderBtn').bind('click', function () {
            var winName = $('#winnerName').textbox('getValue');
            var startD = $('#startDate').datebox('getValue');
            var endD = $('#endDate').datebox('getValue');

            if (startD!=null && startD!='') {
                var flag = (endD!=null && endD!='');
                if (!flag) {
                    showMsg('提示', '结束日期不可以为空');
                    return ;
                }
            }

            var winLogList = $('#winnerLogDg').datagrid('options').queryParams;
            winLogList.uName = winName;
            winLogList.start = startD;
            winLogList.end = endD;
            $('#winnerLogDg').datagrid({'pageNumber':1});
            $('#winnerLogDg').datagrid('reload');
        });

    });

    function fmtControl(val,row) {
        var result = '<span style="color:#cccccc">未中奖</span>';
        switch (parseInt(val)) {
            case 1:
                result = '<span style="color:#ff8c00">已中奖</span>';
                break;
            case 2:
                result = '<span style="color:#008000">已审核</span>';
                break;
            case 3:
                result = '<span style="color:#ff4136">已发放</span>';
                break;
            case 4:
                result = '<span style="color:#e03997">已放弃</span>';
                break;
        }
        return result;
    }
    
    function loadSuccess(data) {

    }

    function onSelected(date){
        $('#endDate').datebox('reset');
    }

    function dateFormatter(date){
        var y = date.getFullYear();
        var m = date.getMonth()+1;
        var d = date.getDate();
        return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
    }

    function updateWinner(status) {
        var winners = $('#winnerLogDg').datagrid('getSelections');
        if (winners==null||winners.length<0) {
            showMsg('操作失败', '请勾择需要审核/发奖的中奖者.');
        } else {
            var wlIds = new Array();
            $(winners).each(function(i, e){
                if(e.dataStatus=='1' && status=='3') {
                    showMsg('操作失败','请先审核中奖信息再发奖!');
                    wlIds.length = 0;
                    return false;
                } else if ((e.dataStatus=='0' || e.dataStatus=='4')) {
                    showMsg('操作失败','未中奖或是弃权的用户无法审核/发奖!');
                    wlIds.length = 0;
                    return false;
                }
                wlIds.push(e.winId);
            });

            if (wlIds.length>0) {
                $.post('${base}/admin/winner/updateWinStatus.do', {wlIds: wlIds.join(','), status: status}, function (data) {
                    if (data.ok) {
                        showMsg('操作成功', '更新用户中奖状态成功!');
                        $('#winnerLogDg').datagrid('reload');
                    } else {
                        showMsg('操作失败', '更新用户中奖状态失败!');
                    }
                }, 'json');
            }
        }
    }

</script>